package org.xhy.sniffer.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class Config {

    // 单例实例
    private static Config instance;

    // 配置文件内容
    private Properties properties;

    // 私有构造函数，防止外部实例化
    private Config() {
        // 加载配置文件
        properties = new Properties();
        String projectRootPath = System.getProperty("user.dir");
        String configFilePath = projectRootPath + File.separator + "sniffer" + File.separator + "config" + File.separator + "app.properties";
        try (FileInputStream fis = new FileInputStream(configFilePath)) {
            properties.load(fis);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 提供一个静态方法来获取单例实例
    public static Config getInstance() {
        if (instance == null) {
            synchronized (Config.class) {
                if (instance == null) {
                    instance = new Config();
                }
            }
        }
        return instance;
    }

    // 提供一个方法来读取配置
    public Properties getProperties() {
        return properties;
    }

    // 根据 key 获取配置项的值
    public static String getProperty(String key) {
        return getInstance().getProperties().getProperty(key);
    }
}
